package com.shujia.scala

object Demo9Apply {
  def main(args: Array[String]): Unit = {


    /**
      * 不适用new创建对象，实际上是调用伴生对象的apply函数
      *
      */
    val dog: Dog = Dog("小狗", 2)

  }
}

class Dog(name: String, age: Int) {

}


//伴生对象

object Dog {
  def fun(): Unit = {
    println("dog")
  }

  /**
    * applyh函数可以直接通过对象名+括号调用
    *
    */

  def apply(name: String, age: Int): Dog = {
    val dog = new Dog(name, age)
    return dog
  }
}
